#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>

int *a;
int q;
int append_list(int a[],int num)
{
	int i;
	printf("Enter the element to append");
	scanf("%d",&num);
	q=q+1;
	a=realloc(a,sizeof(int)*q);
	a[q-1]=num;
	
	printf("array is:");
		for (i=0;i<q;i++)
		{
		printf("%d ",a[i]);
		}
}


int inde(int a[],int q)
{
int r,ind,i;
printf("Enter the number whose position you want to know");
scanf("%d",&ind);
for (i=0;i<q;i++)
{
    if(a[i]==ind)
    {                printf("the position is %d",i);
                     break;
                     }
                     }
}

int extend(int a[],int q)
{
int i,ex;
printf("Enter the number of elements you want to extend");
scanf("%d",&ex);
for(i=q;i<q+ex;i++)
{
printf("Enter the elements");
scanf("%d",&a[i]);
}
printf("array is:");
for (i=0;i<q+ex;i++)
{
printf("%d",a[i]);
}
}

int insert(int a[],int q)
{
	int ins,o,i;
	printf("Enter the position where you want to insert");
	scanf("%d",&ins);
	printf("Enter the element");
	scanf("%d",&o);
	q=q+1;
	a=realloc(a,sizeof(int)*q);
	for (i=q;i>ins-1;i--)
	{
	a[i]=a[i-1];
	}
	a[ins]=o;
	printf("array after inserting is:");
	for (i=0;i<q;i++)
	printf("%d ",a[i]);
}


int count(int a[],int q)
{
	int coun,count=0,i;
	printf("Enter the number which you want to count");
	scanf("%d",&coun);
	for (i=0;i<q;i++)
	{
	if (a[i]==coun)
	   count=count+1;
	}
	printf("number of counts of the number is %d",count);
	printf("\n");
}


int remov(int a[],int q)
{
int j,h,i;
printf("Enter the element you want to remove");
scanf("%d",&h);
	for(i=0;i<q;i++)
	{
		if(a[i]==h)
		{
			break;
		
		}
	}
 q=q-1;
 a=realloc(a,sizeof(int)*q);
 for(j=i;j<=q-1;j++)
{
	a[j]=a[j+1];
}

printf("Final Array is:");
for (i=0;i<=q-1;i++)
	{
	printf(" %d ",a[i]);
	}
}

int pop(int a[],int q)
{
	int pos,j,i;
	printf("enter the position of the element :");
	scanf("%d",&pos);
	q=q-1;
	a=realloc(a,sizeof(int)*q);
   for(j=pos;j<=q-1;j++)
   {
	                    a[j]=a[j+1];
                     }

printf("Array after poping is:");
for (i=0;i<q;i++)
{
printf("%d ",a[i]);
}
}
	


int reverse(int a[],int q)
{
    int j;
    printf("array after sorting is:");
    for (j=q-1;j>=0;j--)
    {
        printf("%d ",a[j]);
    }
}



int sort(int a[],int q)
{
	int i,j,k,c;
	for(i=0;i<=q-1;i++)
	{
		for(j=q-1;j>=i+1;j--)
		{
			if(a[j]>a[j+1])
			{
				c=a[j];
				a[j]=a[j+1];
				a[j+1]=c;
			}
		}
	}
		
	for(k=0;k<=q-1;k++)
	{
		printf("%d ",a[k]);
	}
}



void main()
{
int i,n,num;
printf("Enter the length of array");
scanf("%d",&q);
a=malloc(sizeof(int)*q);

for (i=0;i<q;i++)
{
                 printf("Enter the element");
                 scanf("%d",&a[i]);
}
do{
printf("1append\n");
printf("2index\n");
printf("3extend\n");
printf("4insert\n");
printf("5pop\n");
printf("6count\n");
printf("7remove\n");
printf("8reverse\n");
printf("9sort\n");

printf("Enter choice:");
scanf("%d",&n);

switch(n){
case 1: append_list(a,q);
	break;
case 2: inde(a,q);
	break;
case 3: extend(a,q);
	break;
case 4:	insert(a,q);
	break;
case 5: pop(a,q);
	break;
case 6: count(a,q);
	break;
case 7: remov(a,q);
	break;
case 8: reverse(a,q);
	break;
case 9: sort(a,q);
	break;

printf("enter 10 to exit or 1 to continue:");
scanf("%d",&num);
printf("\n");
}
}while(num!=10);

}







